/*
 * Copyright (c) 2017. 深圳一七网络科技. All rights reserved.
 */

package com.fenxiangle.yueding.feature.home.presenter;

import android.text.TextUtils;

import com.fenxiangle.yueding.entity.bo.FoucsBo;
import com.fenxiangle.yueding.entity.bo.HomeDataBo;
import com.fenxiangle.yueding.entity.bo.HomeFilterBo;
import com.fenxiangle.yueding.entity.bo.UserCenterBo;
import com.fenxiangle.yueding.feature.home.contract.HomeContract;
import com.fenxiangle.yueding.feature.home.dependencies.home.DaggerHomePresenterComponent;

import java.util.List;

import javax.inject.Inject;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * LIJUWEN
 *
 * @email yiyayiyayaoljw@gmail.com
 * @date 2017/4/11 9:31
 */
public class HomePresenter implements HomeContract.Presenter {

    private HomeContract.View mView;


    @Inject

    HomeContract.Model mModel;


    public HomePresenter(HomeContract.View mView) {
        this.mView = mView;
        DaggerHomePresenterComponent.create().inject(this);
    }

    @Override
    public void start() {

    }

    @Override
    public void getHomeData(  int pageNow,
                              int pageSize,String creditScore, String assess, String city, String sex, String age,  String manAuthentication,int type,String screen) {
        HomeFilterBo homeFilterBo=new HomeFilterBo(pageNow,pageSize, creditScore,  assess,  city,  sex,  age,   manAuthentication, type, screen);
        mModel.getHomeData(homeFilterBo)
                .compose(mView.bindToLife())
                .subscribe(new Observer<List<HomeDataBo>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mView.showLoading(d);
                    }

                    @Override
                    public void onNext(List<HomeDataBo> s ) {
                        mView.dismissLoading();
                       if (s!=null&&!s.isEmpty()){
                           mView.showHomeDataSuccess(s);
                       }else {
                           mView.showHomeDataEmpty();
                       }
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoading();
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                    }
                });
    }

    @Override
    public void getHomeMoreData(int pageNow, int pageSize, String creditScore, String assess, String city, String sex, String age,  String manAuthentication,int type,String screen) {
        HomeFilterBo homeFilterBo=new HomeFilterBo(pageNow,pageSize, creditScore,  assess,  city,  sex,  age,    manAuthentication, type, screen);
        mModel.getHomeData(homeFilterBo)
                .compose(mView.bindToLife())
                .subscribe(new Observer<List<HomeDataBo>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(List<HomeDataBo> s ) {
                      mView.showHomeDataMoreSuccess(s);
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.showHomeDataMoreError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                    }
                });
    }

    @Override
    public void attentionFollow(String attentionUserUid) {
        mModel.attentionFollow(new FoucsBo(attentionUserUid))
                .compose(mView.bindToLife())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mView.showLoading(d);
                    }

                    @Override
                    public void onNext(String s ) {
                        mView.dismissLoading();
                        mView.showFollowSuccess();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoading();
                        mView.showFollowError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                    }
                });
    }

    @Override
    public void attentionUnFollow(String attentionUserUid) {
        mModel.attentionUnFollow(new FoucsBo(attentionUserUid))
                .compose(mView.bindToLife())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mView.showLoading(d);
                    }

                    @Override
                    public void onNext(String s ) {
                        mView.dismissLoading();
                        mView.showUnFollowSuccess();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoading();
                        mView.showFollowError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                    }
                });
    }

}
